package Game;

class CoincidenceDetector{
    public static boolean beyondBorder(GameBox game , Group group){
        for (Block block : group.blocks){
            int x = block.position.getX() + group.position.getX();
            int y = block.position.getY() + group.position.getY();

//            System.out.println("Logic   :\t"+Integer.toString(block.position.getX() ) + '\t' + Integer.toString(block.position.getY()));
//            System.out.println("Offset  :\t"+Integer.toString(group.position.getX()) + '\t' + Integer.toString(group.position.getX()));
//            System.out.println("Physical:\t"+Integer.toString(x) + '\t' + Integer.toString(y));
//            System.out.println();

            if (x < 0 || x >= game.getWidth()){
                return true ;
            }
            if (y < 0 || y >= game.getHeight()){
                return true ;
            }
        }

        return false ;
    }


    public static boolean isConcided(GameBox game , Group group){
        for (Block block : group.blocks){
            int x = block.position.getX() + group.position.getX();
            int y = block.position.getY() + group.position.getY();

//            System.out.println("Logic   :\t"+Integer.toString(block.position.getX() ) + '\t' + Integer.toString(block.position.getY()));
//            System.out.println("Offset  :\t"+Integer.toString(group.position.getX()) + '\t' + Integer.toString(group.position.getX()));
//            System.out.println("Physical:\t"+Integer.toString(x) + '\t' + Integer.toString(y));
//            System.out.println();

            /*
            感觉是没必要单独弄边界检测了
            就都集成到了这里
             */
            if (x < 0 || x >= game.getWidth()){
                return true ;
            }
            if ( y >= game.getHeight()){
                //改规则了，不做顶部检测了，因为生成的位置可能在顶部以上
                //和边界检测不一样的地方
                return true ;
            }

            if (game.tags[x][y] == 1){
                return true ;
            }
        }

        return false ;
    };

    public static boolean topCrash(GameBox gameBox ,Group group){
        for (Block block : group.blocks){
            int x = block.position.getX() + group.position.getX();
            int y = block.position.getY() + group.position.getY();
            if ( y < 0 ){
                return true;
            }
        }

        return false;
    }

}

public class CollisionDetector {
    public static boolean crashed(GameBox game , Group group , ActionType action) throws CloneNotSupportedException {
        Group virtualGroup = group.clone();
        virtualGroup.move(action);
//        System.out.println(group.position.getY());
//        System.out.println(virtualGroup.position.getY());
        return CoincidenceDetector.isConcided(game , virtualGroup);
//        return false;
    };

    public static boolean stranded(GameBox game , Group group ) throws CloneNotSupportedException {
        for (Block block : group.blocks){
            int physicalX = block.position.getX() + group.position.getX();
            int physicalY = block.position.getY() + group.position.getY();

            if (physicalY == game.getHeight() - 1){
                return true ;
            }
            else
            if (game.tags[physicalX][physicalY + 1] == 1){
                return true;
            }
        }
        return false;
    };
}
